import { observable, action } from 'mobx';
import { WeaTableNew, WeaForm } from 'comsMobx';
import * as Bctt from '../components/constants/BaseListConstant';

const { TableStore } = WeaTableNew;
const onlySessionKey = Bctt.ONLY_SESSION_KEY;
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
class BaseListStore {
    @observable loading = false;
    @observable titleName = '';
    @observable titleIcon = '';
    @observable rightMenu = [];
    @observable showSearchAd = false;
    @observable searchKey = '';
    @observable searchConditionForm = {};
    @observable searchConditionComponents = {};
    @observable tableStore = {};
    @observable routerParams = {};
    @observable searchInputName = Bctt.TAB_SEARCH_INPUT_NAME;
    initDatas = {};

    getTableData(requestParams, callBackFn) {

    }

    reLoad = () => {
      this.tableStore.getDatas(this.tableStore.dataKey, this.tableStore.current, this.tableStore.pageSize, this.tableStore.sortParams);
    }

    @action
    componentWillMount=(componentParams) => {
      this.routerParams = componentParams;
      this.searchConditionForm = new WeaForm();
      this.tableStore = new TableStore();
    }

    @action
    componentDidMount=() => {
      this.loading = true;
      const requestParams = {};
      requestParams[onlySessionKey] = false;
      this.getTableData(requestParams, this.componentInitCallBack);
    }

    @action
    componentWillUnmount=() => {
      this.loading = false;
      this.titleName = '';
      this.titleIcon = '';
      this.rightMenu = [];
      this.showSearchAd = false;
      this.searchKey = '';
      this.tableStore = {};
      this.routerParams = {};
      this.searchConditionForm = {};
      this.searchConditionComponents = {};
    }

    @action
    doSearch=(params = {}) => {
      this.loading = true;
      this.setShowSearchAd(false);
      const initParams = {};
      initParams[onlySessionKey] = true;
      initParams[this.searchInputName] = this.searchKey;
      const searchParamsAd = this.searchConditionForm.getFormParams();
      const requestParams = { ...initParams, ...searchParamsAd, ...params };
      for (const item in requestParams) {
        if (requestParams[item] == '') {
          delete requestParams[item];
        }
      }
      this.getTableData(requestParams, this.doSearchCallBack);
    }

    @action
    componentInitCallBack=(data = {}) => {
      this.initDatas = data;
      this.searchConditionForm = new WeaForm();
      const content = data.datas.content;
      this.titleName = content.titleName;
      this.titleIcon = content.titleIcon;
      this.rightMenu = content.rightMenu;
      this.searchConditionComponents = content.searchConditionComponents;
      // 初始化高级搜索
      this.searchConditionForm.initFormFields(content.searchConditionComponents);
      // 初始化表格数据
      this.tableStore.getDatas(content.sessionKey, 1);
      this.loading = false;
    }

    @action
    doSearchCallBack=(data = {}) => {
      const content = data.datas.content;
      this.tableStore.getDatas(content.sessionKey, 1);
      this.loading = false;
    }

    @action
    setShowSearchAd=(bool = false) => {
      this.showSearchAd = bool;
    }

    @action
    setSearchKey=(v = '') => {
      this.searchKey = v;
      try {
        const searchKeyField = {};
        searchKeyField[this.searchInputName] = { value: v };
        this.searchConditionForm.updateFields(searchKeyField, false);
      } catch (error) {
        // 高级搜索中没有searchKey的情况
      }
    }

    @action
    searchConditionFieldChange=(fieldObj) => {
      if (fieldObj[this.searchInputName]) {
        this.searchKey = fieldObj[this.searchInputName].value;
      }
    }

    clearAdFields=() => {
      this.searchConditionForm.reset();
      this.searchKey = '';
    }

    resetAdFields=() => {
      this.searchConditionForm.reset();
      this.searchKey = '';
      const content = this.initDatas.datas.content;
      // 初始化高级搜索
      this.searchConditionForm.initFormFields(content.searchConditionComponents);
    }

    excelExport(excelExportCallBack) {

    }
}

export default BaseListStore;
